<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.title" placeholder="关键词" clearable/>
        </el-form-item>
        <el-form-item label="类型：">
          <el-select size="mini" style="margin-right: 1rem;" v-model="listPram.type" placeholder="请选择"
                     default-first-option>
            <el-option label="巡察公告" value="1"></el-option>
            <el-option label="巡察动态" value="2"></el-option>
            <el-option label="反映须知" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0,null)"
                     v-has-permi="['admin:article:save']">添加
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column align="center" label="ID" prop="id" width="80"></el-table-column>
        <el-table-column align="center" label="标题" prop="title" min-width="150"/>
        <el-table-column align="center" label="类型" prop="typeText" min-width="100"/>
        <el-table-column align="center" label="状态" prop="status" min-width="100">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                :active-value="false"
                :inactive-value="true"
                style="width:40px;"
                @change="handleStatusChange($event,scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createTime" min-width="100"/>
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="handlerOpenEdit(1,scope.row,true)"
                       v-has-permi="['admin:article:save']">编辑
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:article:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[10,20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialogConfig.visible"
        :title="editDialogConfig.isCreate ===1? '编辑':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        top="10px"
        width="1200px">
      <edit
          :view="editDialogConfig.view"
          v-if="editDialogConfig.visible"
          :is-create="editDialogConfig.isCreate"
          :edit-data="editDialogConfig.editData"
          @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import {deleteRow, list, updateStatus} from "@/api/article";
import {checkPermi} from "@/utils/permission";

export default {
  components: {edit},
  data() {
    return {
      listData: {list: [], total: 0},
      listPram: {
        title: '',
        type: null,
        page: 1,
        limit: 10,
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {},
        view: false,
      },
      tableLoading: false,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    handlerOpenEdit(isCreate, editDate, view = false) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
      this.editDialogConfig.view = view;
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.editDialogConfig.view = false;
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.title = '';
      this.listPram.type = null;
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
